package com.example.testpic;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.example.testpic.BitmapCache.NativeImageCallBack;
//import com.example.testpic.BitmapCache.ImageCallback;
import com.example.testpic.ImageGridAdapter.TextCallback;

public class ImageGridActivity extends Activity {
	final String TAG = getClass().getSimpleName();
	public static final String EXTRA_IMAGE_LIST = "imagelist";
	List<ImageItem> dataList;
	GridView gridView;
	ImageGridAdapter adapter;
	TextView imageCatalogView, sendText, previewTextView;

//	public ImageGridActivity() {
//		bitmapCache = BitmapCache.getCacheInstance(this);
//	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_image_grid);
		dataList = (List<ImageItem>) getIntent().getSerializableExtra(
				EXTRA_IMAGE_LIST);

		imageCatalogView = (TextView) findViewById(R.id.image_catalog);
		sendText = (TextView) findViewById(R.id.send);
		previewTextView = (TextView) findViewById(R.id.preview_tv);
		initView();
		imageCatalogView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ImageGridActivity.this,AlbumListActivity.class);
				ImageGridActivity.this.startActivity(intent);
				ImageGridActivity.this.finish();
			}
		});
		sendText.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent resultIntent = new Intent(ImageGridActivity.this,
						SendPicActivity.class);
				resultIntent.putExtra("selectedImages",
						(Serializable) AlbumListActivity.pathList);
				startActivity(resultIntent);
				AlbumListActivity.map.clear();
				AlbumListActivity.pathList.clear();
				AlbumListActivity.imageSelectedTotal = 0;
				ImageGridActivity.this.finish();
			}
		});
		previewTextView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(ImageGridActivity.this,
						ImagePreviewActivity.class);
				if (AlbumListActivity.map.size() > 0) {
					startActivity(intent);
				} else {
					Toast.makeText(ImageGridActivity.this, "请选择要预览的图片",
							Toast.LENGTH_LONG).show();
				}
			}
		});
	}

	private void initView() {
		gridView = (GridView) findViewById(R.id.gridview);
		gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
		adapter = new ImageGridAdapter(ImageGridActivity.this, dataList,
				gridView);
		gridView.setAdapter(adapter);
		if (AlbumListActivity.imageSelectedTotal == 0) {
			sendText.setText("发送");
		} else {
			sendText.setText("发送" + "(" + AlbumListActivity.imageSelectedTotal
					+ ")");
		}
		adapter.setTextCallback(new TextCallback() {
			public void onListen(int count) {
				if (count == 0)
					sendText.setText("发送");
				else
					sendText.setText("发送" + "(" + count + ")");
			}
		});

		gridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				adapter.notifyDataSetChanged();
			}
		});

	}
}
